/*
  5.26截止
  待办：
  我方飞机，敌方boss和各种单位的第一次动画效果不能播放 √
  时间长会卡顿，游戏总元素集合大小突然变大问题 √
  游戏各项参数的优化
  炸弹发射效果制作
  重新开始，结束游戏的制作
  我方飞机与敌方飞机碰撞敌方飞机爆炸动画的效果制作 √
  敌方二号飞机发射子弹动画 √
  道具bug解决 √
 */
/*
  做个功能：
  1. 敌方的三种飞机位置全都受到限制  --- 解决
  2. 地方的飞机在画面中间莫名消失
 */
package com.sxt;

import OwnSideObject.BulletObj;
import OwnSideObject.PlaneObj;
import PropsObject.BombPropsObj;
import PropsObject.BulletPropsObj;
import PropsObject.PropsObject;
import connect.Cliant;
import enemyObject.*;

import javax.sound.sampled.Clip;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 1. “游戏结束” 和 “游戏胜利”的界面有问题
 */

public class GameWin  extends JFrame {

    public static AudioClip music;
    public static Clip clip;
    public static URL url;
    public static AudioClip ac;
    public static Boolean dight=false;


    Cliant cliant=new Cliant();
    //定义双缓存图片
    //创建和容器一样大小的Image图片
    public Image offScreenImage = this.createImage(WINDOW_WIDTH, WINDOW_HEIGHT);
    //道具存在标识
    public boolean PropsisExist = false;
    //boss死亡需要的绘图帧数
    public static int bossDead = 6*2;
    //我方飞机死亡需要的绘图帧数
    public static int MyPlaneDead = 4;
    public static int count=1;
    //游戏重绘次数
    //第一次启动游戏
    boolean FirstLaunch = true;
    //将所有数据变量化
    int FPS = 100;     //游戏帧数
    int sleepTime = 1000 / FPS;    //睡眠时间
    int PropsCD = PROPS_CD;
    //暂停开始按键
    boolean suspensionPlay = false; //暂停状态位
    boolean resumePlay = false; //重新开始状态位
    boolean pushFlag = true;

    static int PROPS_CD = 1000;//道具CD
    public static int WINDOW_WIDTH = 600; //窗口的宽度
    public static int WINDOW_HEIGHT = 800; //窗口的长度
    static int AIRCRAFT_BATTLE_WIDTH = 156; //飞机大战字样的宽度
    static int AIRCRAFT_BATTLE_HEIGHT = 37; //飞机大战字样的高度
    static int TOP_BORDER_HEIGHT = 30; //GUI窗口的上边框高度
    static int BACKGROUND_IMAGE_WIDTH = 2409; //背景图的宽度
    static int BACKGROUND_IMAGE_HEIGHT = 2126; //背景图的长度
    static int WINDOW_LOCATION_X = 0; //窗口左上角的X坐标，初始设为0
    static int WINDOW_LOCATION_Y = 0; //窗口左上角的Y坐标，初始设为0
    static int SUSPENSION_LOGO_WIDTH = 60; //暂停键的宽度
    static int SUSPENSION_LOGO_HEIGHT = 45; //暂停键的长度
    static int RESUME_LOGO_WIDTH = 60; //恢复键的宽度
    static int RESUME_LOGO_HEIGHT = 45; //恢复键的长度
    static int OPTION_LOGO_SIDE_LENGTH = 100; //初始界面三个选项的边长大小
    static int FAIL_BUTTON_WIDTH = 300; //游戏失败后出现的按钮的宽度
    static int FAIL_BUTTON_HEIGHT = 41; //游戏失败之后出现的按钮的高度
    static int LOGGED_IN_INTERVAL_LENGTH = (WINDOW_WIDTH - 3 * OPTION_LOGO_SIDE_LENGTH) / 4; //已经登陆后三个选项之间的距离
    static int NOT_LOGGED_IN_INTERVAL_LENGTH = (WINDOW_WIDTH - 4 * OPTION_LOGO_SIDE_LENGTH) / 5; //没有登陆时三个选项之间的距离
    static int GAME_OVER_TEX_WIDTH = 336; //游戏结束时出现的文字图片的宽度
    static int GAME_OVER_TEX_HEIGHT = 82; //游戏结束时出现的文字图片的高度

    static int RANK_LIST_DIALOG_WIDTH = 200; //排行榜弹窗的宽度
    static int RANK_LIST_DIALOG_HEIGHT = 200; //排行榜弹窗的高度
    static int ABOUT_US_DIALOG_WIDTH = 200; //关于我们弹窗的宽度
    static int ABOUT_US_DIALOG_HEIGHT = 200; //关于我们弹窗的高度
    static int LOGGED_IN_DIALOG_WIDTH = 200; //登录弹窗的宽度
    static int LOGGED_IN_DIALOG_HEIGHT = 200; //登录弹窗的高度
    static int REGISTER_DIALOG_WIDTH = 200; //注册弹窗的高度
    static int REGISTER_DIALOG_HEIGHT = 200; //注册弹窗的高度
    static int RETURN_MESSAGE_DIALOG_WIDTH = 100; //输入信息后回应的窗口的宽度
    static int RETURN_MESSAGE_DIALOG_HEIGHT = 80; //输入信息后回应的窗口的高度

    public char plane_choice = 'a'; //飞机型号选择
    public int mode = 0; //游戏模式
    public static int ENEMY_VANISH_LOCATION_X = -300; //敌机消失的时候的位置X
    public static int ENEMY_VANISH_LOCATION_Y = 300; //敌机消失的时候的位置Y
    public static int MY_BULLET_VANISH_LOCATION_X = -300; //我方子弹消失的时候的位置X
    public static int MY_BULLET_VANISH_LOCATION_Y = -300; //我方子弹消失的时候的位置Y

    public static int RANK_LIST_BUTTON_WIDTH = 100;
    public static int RANK_LIST_BUTTON_HEIGHT = 39;
    public static int RANK_LIST_RETURN_WIDTH = 76;
    public static int RANK_LIST_RETURN_HEIGHT = 37;

    //游戏状态：0未开始 1运行中 2暂停 3失败 4成功
    public int status = 0; //初始状态为未开始状态
    public int status_backup;
    boolean haveStartedGame = false;
    static boolean haveLoggedIn = false; //是否已经登录账号,修改为static是为了MyInterDialog可以修改登录状态
    Image background = Toolkit.getDefaultToolkit().getImage("images/backgroundNew.png"); //背景图片
    Image gameFailed = Toolkit.getDefaultToolkit().getImage("images/gameFailed.png"); //任务失败
    Image gameSucceeded = Toolkit.getDefaultToolkit().getImage("images/gameSucceeded.png"); //任务成功
    //敌方boss爆炸效果
    //计算地方1号飞机生成数量
    int enemy1Count = 0;
    //计算敌方2号飞机的生成数量
    int enemy2Count = 0;
    //敌方boss
    public BossObj bossObj = null;
    //boss三连发
    int threeCount = 3;
    //背景的实体类
    public BacGroundObj bacGroundObj = new BacGroundObj("images/backgroundNew.png", 0, WINDOW_HEIGHT - BACKGROUND_IMAGE_HEIGHT + TOP_BORDER_HEIGHT, 1, this);

    //我方子弹的集合
    public List<BulletObj> bulletObjList = new CopyOnWriteArrayList<>();
    //敌方子弹集合
    public List<EnemyBulletObj> enemyBulletObjs = new CopyOnWriteArrayList<>();
    //定义大集合：敌方子弹，我方子弹，敌方飞机，我方飞机
    public List<GameObject> gameObjectList = new CopyOnWriteArrayList<>();
    //定义敌方大集合
    public List<EnemyObject> enemyObjList = new CopyOnWriteArrayList<>();
    //敌方战斗机集合
    public List<EnemyObj> enemy1ObjList = new CopyOnWriteArrayList<>();
    public List<Enemy2Obj> enemy2ObjList = new CopyOnWriteArrayList<>();
    //被删除物体的集合
    public List<GameObject> removeList = new CopyOnWriteArrayList<>();
    //动画对象的集合
    public List<AnimationObj> animationObjs = new CopyOnWriteArrayList<>();
    //道具集合
    public List<BombPropsObj> bombPropsObjs = new CopyOnWriteArrayList<>();
    public List<BulletPropsObj> bulletPropsObjs = new CopyOnWriteArrayList<>();
    //发射道具集合
    public List<BombPropsObj> props_launchList = new CopyOnWriteArrayList<>();
    //暂停按键和恢复按键
    ControlsObj suspension = new ControlsObj("images/pause_nor.png",WINDOW_WIDTH - SUSPENSION_LOGO_WIDTH,30,0,this); //暂停按键
    ControlsObj resume = new ControlsObj("images/resume_nor.png", WINDOW_WIDTH / 2 - RESUME_LOGO_HEIGHT / 2,WINDOW_HEIGHT / 2 - RESUME_LOGO_HEIGHT / 2,0,this); //恢复按键
    //有五个按钮，分别是：开始游戏、查看排行榜、关于我们
    MyButton rankListButton = new MyButton("images/rankList.png", 0, (int) (WINDOW_HEIGHT * 0.6), 0, this);
    MyButton startGameButton = new MyButton("images/startGame.png", 0, (int) (WINDOW_HEIGHT * 0.6) + 20, 0, this); //这个20是为了看起来更好看点
    MyButton aboutUsButton = new MyButton("images/aboutUs.png", 0, (int) (WINDOW_HEIGHT * 0.6), 0, this);
    MyButton loggedInButton = new MyButton("images/loggedIn.png", 0, (int) (WINDOW_HEIGHT * 0.6), 0, this);
    MyButton registerButton = new MyButton("images/register.png", 0, (int) (WINDOW_HEIGHT * 0.6), 0, this);
    GameOverButton gameOverButton = new GameOverButton("images/gameover.png", WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 - 50, 0, this);
    GameOverButton againButton = new GameOverButton("images/again.png", WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2, 0, this);
    MyButton normalButton = new MyButton("images/normalButton_1.png",WINDOW_WIDTH / 2-130,startGameButton.y-80,0,this);
    MyButton endlessButton = new MyButton("images/endlessButton_1.png",WINDOW_WIDTH / 2+30,startGameButton.y-80,0,this);
    MyButton rankListNormalButton = new MyButton("images/normalButton_1.png", WINDOW_WIDTH / 2 - RANK_LIST_BUTTON_WIDTH, TOP_BORDER_HEIGHT, 0, this);
    MyButton rankListEndlessButton = new MyButton("images/endlessButton_1.png", WINDOW_WIDTH / 2, TOP_BORDER_HEIGHT, 0, this);
    MyButton rankListReturnButton = new MyButton("images/rankListReturn.png", WINDOW_WIDTH / 2 - RANK_LIST_RETURN_WIDTH / 2, WINDOW_HEIGHT - RANK_LIST_RETURN_HEIGHT - 30, 0, this);


    //“飞机大战”字样
    Image aircraftBattleImage = Toolkit.getDefaultToolkit().getImage("images/AircraftWar.png");
    //状态为0的时候画面中的动态飞机
    AnimationObj beginAnimationPlane = new AnimationObj("images/me_"+plane_choice,".png", 2, WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2, WINDOW_HEIGHT / 3 - TOP_BORDER_HEIGHT, this);
    //我方战斗机
    public PlaneObj planeObj = new PlaneObj("images/me_"+plane_choice+".png", WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2, WINDOW_HEIGHT - PlaneObj.MY_PLANE_HEIGHT, 0, this,1);
    MyButton leftButton = new MyButton("images/leftButton.png",WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2-50, WINDOW_HEIGHT / 3 - TOP_BORDER_HEIGHT+20,0, this);
    MyButton rightButton = new MyButton("images/rightButton.png",WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2+100, WINDOW_HEIGHT / 3 - TOP_BORDER_HEIGHT+20,0, this);
    //登陆成功之后左上角的“我的信息”按钮
    MyButton myInformationButton = new MyButton("images/myInformation.png", 5, TOP_BORDER_HEIGHT, 0, this);

    String[][] rankListStringArray = new String[10][2];
    String string1, string2;

    //窗口的启动方法
    public void launch() throws IOException {
        //窗口是否可见
        setVisible(true);
        //窗口大小
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        //窗口的位置
        setLocationRelativeTo(null); //居中
        WINDOW_LOCATION_X = getLocation().x;
        WINDOW_LOCATION_Y = getLocation().y;
        //窗口的标题
        setTitle("飞机大战");
        //当点击"x"的时候，程序停止
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //将游戏物体添加到大的集合
        gameObjectList.add(bacGroundObj); //背景图片
        gameObjectList.add(planeObj); //我方飞机
        gameObjectList.add(suspension); //暂停按钮

        BufferedReader bufferedReader = new BufferedReader(new FileReader("Normal.txt"));
        int i= 0;
        while((string1 = bufferedReader.readLine()) != null && (string2 = bufferedReader.readLine()) != null){
            rankListStringArray[i][0] = string1;
            rankListStringArray[i][1] = string2;
            i++;
        }
        bufferedReader.close();

//        为窗口添加开始鼠标事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
//                  e.getLocationOnScreen() //这个函数只能返回相对于屏幕上的点
                Point point = e.getLocationOnScreen();
                point.setLocation(point.x - WINDOW_LOCATION_X, point.y - WINDOW_LOCATION_Y);

                //在游戏还没开始的条件下
                if (status == 0) {
                    if(haveLoggedIn && myInformationButton.getRec().contains(point)){
                        //显示个人信息
                        JFrame myInformation = new JFrame("个人信息");
                        myInformation.setLayout(new GridLayout());
                        myInformation.setBounds(600,400,100,100);
                        JTextField name = new JTextField("用户名：");
                        name.setEditable(false);
                        JTextField score_max = new JTextField("最高得分：");
                        score_max.setEditable(false);
                        myInformation.add(name);
                        myInformation.add(score_max);
                        myInformation.setVisible(true);
                    } else if (startGameButton.getRec().contains(point) && haveLoggedIn) {
                        if(mode == 1){
                            //普通模式
                            status = 1;
                            status_backup=status;
                        } else if(mode == 2){
                            //无尽模式
                            status = 5;
                            status_backup=status;
                        } else JOptionPane.showMessageDialog(GameWin.this,"请先选择模式","开始游戏",1);
                    } else if (rankListButton.getRec().contains(point)) {
                        //显示排行榜
                        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 这里的message需要修改
                        status = 6;
                    } else if (aboutUsButton.getRec().contains(point)) {
                        //显示“关于我们”
                        String about_us = "本产品为飞机大战5.0\n我们的仓库是……\n如果有任何问题请拨打…… 或者发送到我们的邮箱：……";
                        JOptionPane.showMessageDialog(GameWin.this, about_us, "关于我们", 1);
                    } else if (loggedInButton.getRec().contains(point) && !haveLoggedIn) {
                        //显示登录界面
                        MyInteractDialog myDialog = new MyInteractDialog(LOGGED_IN_DIALOG_WIDTH, LOGGED_IN_DIALOG_HEIGHT, "登录", "用户名", "密码", "登录", "退出","");
                        myDialog.showDialogRegister(); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 这里需要对接修改
                    } else if (registerButton.getRec().contains(point) && !haveLoggedIn) {
                        //显示注册界面
                        MyInteractDialog myDialog = new MyInteractDialog(REGISTER_DIALOG_WIDTH+120, REGISTER_DIALOG_HEIGHT+60, "注册", "用户名", "密码", "注册", "退出","");
                        myDialog.showDialogEnroll(); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  这里需要对接修改
                    }
                    else if(leftButton.getRec().contains(point)&&haveLoggedIn){
                        if(plane_choice!='a'){
                            plane_choice -= 1;
                        }
                        else JOptionPane.showMessageDialog(GameWin.this,"没有更多了！","飞机样式",1);
                    }
                    else if(rightButton.getRec().contains(point)&&haveLoggedIn){
                        if(plane_choice!='g'){
                            plane_choice+=1;
                        }
                        else JOptionPane.showMessageDialog(GameWin.this,"没有更多了！","飞机样式",1);
                    }
                    else if(normalButton.getRec().contains(point)&&haveLoggedIn){
                        normalButton = new MyButton("images/normalButton_2.png",WINDOW_WIDTH / 2-130,startGameButton.y-60,0,GameWin.this);
                        endlessButton = new MyButton("images/endlessButton_1.png",WINDOW_WIDTH / 2+30,startGameButton.y-60,0,GameWin.this);
                        mode = 1;
                    }
                    else if(endlessButton.getRec().contains(point)&&haveLoggedIn){
                        endlessButton = new MyButton("images/endlessButton_2.png",WINDOW_WIDTH / 2+30,startGameButton.y-60,0,GameWin.this);
                        normalButton = new MyButton("images/normalButton_1.png",WINDOW_WIDTH / 2-130,startGameButton.y-60,0,GameWin.this);
                        mode = 2;
                    }
                }
                //游戏失败之后
                if(status == 3){
                    if(gameOverButton.getRec().contains(point)){
                        //游戏结束
                        status = 0;
                        haveLoggedIn=true;
                        PropsisExist = false;
                        count = 1;
                        haveStartedGame = false;
                        enemy1Count = 0;
                        enemy2Count = 0;
                        planeObj = new PlaneObj("images/me1.png", WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2, WINDOW_HEIGHT - PlaneObj.MY_PLANE_HEIGHT, 0, GameWin.this,1);
                        BacGroundObj.score = 0;
                        bulletObjList.clear();
                        enemyBulletObjs.clear();
                        gameObjectList.clear();
                        enemyObjList.clear();
                        enemy1ObjList.clear();
                        enemy2ObjList.clear();
                        removeList.clear();
                        animationObjs.clear();
                        bulletPropsObjs.clear();
                        bombPropsObjs.clear();
                        props_launchList.clear();
                        PlaneObj.isDestroyed = false;
                        MyPlaneDead = 4;
                        bossDead = 12;
                        bossObj = null;
                        gameObjectList.add(bacGroundObj); //背景图片
                        gameObjectList.add(planeObj); //我方飞机
                        gameObjectList.add(suspension); //暂停按钮
                    }else if(againButton.getRec().contains(point)){
                        gameAgain();
                    }
                }
                //监听
                String str1, str2;
                if(status == 6){
                    if(rankListNormalButton.getRec().contains(point)){
                        //展示普通模式排行榜
                        try {
                            BufferedReader bufferedReader1 = new BufferedReader(new FileReader("Normal.txt"));
                            int i = 0;
                            while((str1 = bufferedReader1.readLine()) != null && (str2 = bufferedReader1.readLine()) != null){
                                rankListStringArray[i][0] = str1;
                                rankListStringArray[i][1] = str2;
                                i++;
                            }
                            bufferedReader1.close();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    else if(rankListEndlessButton.getRec().contains(point)){
                        //展示无尽模式的排行榜
                        try {
                            BufferedReader bufferedReader1 = new BufferedReader(new FileReader("Endless.txt"));
                            int i = 0;
                            while((str1 = bufferedReader1.readLine()) != null && (str2 = bufferedReader1.readLine()) != null){
                                rankListStringArray[i][0] = str1;
                                rankListStringArray[i][1] = str2;
                                i++;
                            }
                            bufferedReader1.close();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    else if(rankListReturnButton.getRec().contains(point)){
                        //返回
                        status = 0;
                    }
                }
            }
        });

        //为暂停添加一个键盘事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                //空格键被按下
                if (e.getKeyCode() == 32){
                    switch (status) {
                        //运行改暂停
                        case 1:
                            status = 2;
                            break;
                        //暂停改运行
                        case 2:
                            resumePlay = true;
                            suspensionPlay = false;
                            break;
                        case 5:
                            status = 2;
                            break;
                    }
                }
                //换行键被按下，发射炸弹
                if (e.getKeyCode()==10&&planeObj.bombNum>0){
                    planeObj.bombNum--;
                    bomb_launch();
                }
            }
        });



        while (true) {
            long startTime = System.currentTimeMillis();
            //建议系统释放无用内存
            System.gc();
            //当removeList到达一定大小后手动清理removeList
            if (removeList.size()>100)
                removeList.clear();

            //帧数计数
            count++;
            //每一帧都重新加载窗口的大小
            WINDOW_LOCATION_X = this.getLocation().x;
            WINDOW_LOCATION_Y = this.getLocation().y;
            //每一帧都重新加载窗口的长度和宽度
            WINDOW_WIDTH = this.getWidth();
            WINDOW_HEIGHT = this.getHeight();
            LOGGED_IN_INTERVAL_LENGTH = (WINDOW_WIDTH - 3 * OPTION_LOGO_SIDE_LENGTH) / 4; //已经登陆后三个选项之间的距离
            NOT_LOGGED_IN_INTERVAL_LENGTH = (WINDOW_WIDTH - 4 * OPTION_LOGO_SIDE_LENGTH) / 5; //没有登陆时三个选项之间的距离

            //仅当游戏正在运行时才添加子弹
//            System.out.println("status: " + status);
            repaint();

            if(bossDead<=0 && status != 5)  //游戏成功
                this.status=4;
            if (MyPlaneDead<0) //游戏失败
                this.status=3;
            //如果敌方boss已经死亡，开始游戏倒计时
            if (!BossObj.isAlive && status != 5)
                bossDead--;
            //如果我方飞机已经死亡，开始倒计时
            if (PlaneObj.isDestroyed){
                MyPlaneDead--;
            }

            //仅当游戏正在运行时才添加子弹
            if (status == 1 || status == 5) {
                PropsCD--;
                if (gameObjectList.size()<50)
                    createObj();
                System.out.println("游戏所有元/素集合的大小：" + gameObjectList.size());
                System.out.println("---------------------------------------------------当前FPS:"+FPS);
//                System.out.println("删除集合中元素个数："+removeList.size());
//                System.out.println("动画集合的元素个数："+animationObjs.size());
                System.out.println("道具CD："+PropsCD);
            }
            try {
                //线程休眠
                Thread.sleep(sleepTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("循环运行时间："+(endTime-startTime));
        }
    }

    void bomb_launch(){
        GameMusic.use_bomb.play();
        BulletObj launch_bomb = new BulletObj("images/bomb_launch.png",planeObj.x+51,planeObj.y,5,this,2);
        gameObjectList.add(launch_bomb);
    }

    @Override
    public void paint(Graphics g) {

        //创建和容器一样大小的Image图片
        offScreenImage = this.createImage(WINDOW_WIDTH, WINDOW_HEIGHT);
        //获得该图片的画布
        Graphics gImage = offScreenImage.getGraphics();
        //填充整个画布
        gImage.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

        rankListNormalButton.setLocation(WINDOW_WIDTH / 2 - RANK_LIST_BUTTON_WIDTH, TOP_BORDER_HEIGHT);
        rankListEndlessButton.setLocation(WINDOW_WIDTH / 2, TOP_BORDER_HEIGHT);
        rankListReturnButton.setLocation(WINDOW_WIDTH / 2 - RANK_LIST_RETURN_WIDTH / 2, WINDOW_HEIGHT - RANK_LIST_RETURN_HEIGHT - 30);

        //游戏未开始
        if (status == 0) {
            //背景图
            gImage.drawImage(background, 0, 0, null);
            //飞机大战字样
            gImage.drawImage(aircraftBattleImage, WINDOW_WIDTH / 2 - AIRCRAFT_BATTLE_WIDTH / 2, (WINDOW_HEIGHT) / 20 + TOP_BORDER_HEIGHT, null);
            //改变文字颜色
            gImage.setColor(Color.blue);
            //改变文字大小
            gImage.setFont(new Font("楷体", Font.BOLD, 20));
            //飞机动态图
            beginAnimationPlane.paintSelfLoop(gImage);

            if(haveLoggedIn){
                myInformationButton.paintSelf(gImage);
                leftButton.setLocation(WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2 - 50, WINDOW_HEIGHT /3 - TOP_BORDER_HEIGHT + 20);
                rightButton.setLocation(WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2 + 100,WINDOW_HEIGHT / 3- TOP_BORDER_HEIGHT + 20);
                leftButton.paintSelf(gImage);
                rightButton.paintSelf(gImage);
                beginAnimationPlane = new AnimationObj("images/me_"+plane_choice,".png", 2, WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2, WINDOW_HEIGHT / 3 - TOP_BORDER_HEIGHT, this);
                normalButton.setLocation(WINDOW_WIDTH / 2 - 130, startGameButton.y - 80);
                endlessButton.setLocation(WINDOW_WIDTH / 2 + 30, startGameButton.y - 80);
                normalButton.paintSelf(gImage);
                endlessButton.paintSelf(gImage);
            }

            //绘制所有的按钮
            printButton0(gImage);

        }
        if (status==2){
            if (!suspensionPlay&&!resumePlay){  //从游戏运行进入游戏暂停，打印一次按压的暂停按钮后设置游戏暂停状态
                music.stop();
                suspensionPlay=true;
            }
            if (suspensionPlay&&!resumePlay){   //游戏已经进入暂停状态，打印resume按钮的同时继续显示游戏背景
                bacGroundObj.paintSelf(gImage);
                resume.paintSelf(gImage);
            }
            if (!suspensionPlay&&resumePlay){   //从游戏暂停状态重回游戏开始状态，将状态设置回为1，并且将两个用于暂停的标志归位
                music.loop();
                resumePlay = false;
                status = status_backup;
//                if(mode == 1)
//                    status = 1;
//                else if(mode == 2)
//                    status = 5;
            }
        }
        if (status == 1) {
            if(dight==false){
                dight=true;
                File f1 = new File("D:\\ComputerLearning\\TYUT_Computer\\AircraftWar_\\sound\\【FF14】精神错乱（无瑕灵君BGM） 摇滚版 - 1.Final Fantasy XIV Shadowbringers goes (Av926678268,P1).wav");
                try {
                    url= f1.toURI().toURL();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                //           URL musicPath = GameWin.class.getResource("C:\\Users\\www\\Desktop\\AircraftWar\\sound\\game_music.wav");
                music= Applet.newAudioClip(url);
                if(dight==true){
                    music.loop();
                }

            }
            if (FirstLaunch){
                FirstLaunch = false;
                gameObjectList.add(new BossObj("images/enemy3_n1.png", -200, -200, 0, this,2));
                gameObjectList.add(new Enemy2Obj("images/enemy2.png",-200,-200,0, this,2));
                gameObjectList.add(new EnemyObj("images/enemy1.png",-200,-200,0,this,2));
                gameObjectList.add(new PlaneObj("images/me1.png", -200, -200, 0, this,2));
            }
            //重新设定背景图应该在的高度
            if(!haveStartedGame) {
                suspension.setLocation(WINDOW_WIDTH - SUSPENSION_LOGO_WIDTH, 30);
                resume.setLocation(WINDOW_WIDTH / 2 - RESUME_LOGO_HEIGHT / 2, WINDOW_HEIGHT / 2 - RESUME_LOGO_HEIGHT / 2);
                bacGroundObj.setY(WINDOW_HEIGHT - BACKGROUND_IMAGE_HEIGHT + TOP_BORDER_HEIGHT); //背景重新设置Y值
                haveStartedGame = true;
            }
            //绘制所有游戏元素
            //将所有敌方元素添加
            enemyObjList.addAll(enemy1ObjList);
            enemyObjList.addAll(enemy2ObjList);
            //添加动画元素
            gameObjectList.addAll(animationObjs);
            for (GameObject object : gameObjectList){
                try {
                    object.paintSelf(gImage);
                }catch (Exception e){
                    object.DeBug();
                    e.printStackTrace();
                }

            }
            for (BombPropsObj propsObj:props_launchList)
                propsObj.paintSelf_launch(gImage);
            gameObjectList.removeAll(removeList);
            bulletObjList.removeAll(removeList);
            enemyBulletObjs.removeAll(removeList);
            enemyObjList.removeAll(removeList);
            enemy1ObjList.removeAll(removeList);
            enemy2ObjList.removeAll(removeList);
            animationObjs.removeAll(removeList);
            bombPropsObjs.removeAll(removeList);
            bulletPropsObjs.removeAll(removeList);
            props_launchList.removeAll(removeList);
        }
        //游戏失败
        if (status == 3) {
            music.stop();
            gImage.drawImage(background, 0, 0, null);
            printButton3(gImage);
            gImage.drawImage(gameFailed, WINDOW_WIDTH / 2 - GAME_OVER_TEX_WIDTH / 2, WINDOW_HEIGHT / 8, null); //有待调整
            gImage.setFont(new Font("微软雅黑", Font.BOLD, 50));
            gImage.drawString("您的分数:" + BacGroundObj.score, WINDOW_WIDTH / 2 - 120, WINDOW_HEIGHT / 3 + 50);
            if (pushFlag){
                pushFlag=false;
                Cliant cliant=new Cliant();
                try {
                    cliant.set(MyInteractDialog.userNameTextField.getText(),BacGroundObj.score,status_backup);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (status == 4) {
            music.stop();
            gImage.drawImage(background, 0, 0, null);
            printButton3(gImage);
            gImage.drawImage(gameSucceeded, WINDOW_WIDTH / 2 - GAME_OVER_TEX_WIDTH / 2, WINDOW_HEIGHT / 8, null); //有待调整
            gImage.setFont(new Font("微软雅黑", Font.BOLD, 50));
            gImage.drawString("您的分数:" + BacGroundObj.score, WINDOW_WIDTH / 2 - 120, WINDOW_HEIGHT / 3 + 50);
            if (pushFlag){
                pushFlag=false;
                Cliant cliant=new Cliant();
                try {
                    cliant.set(MyInteractDialog.userNameTextField.getText(),BacGroundObj.score,status_backup);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if(status == 5){
            System.out.println("无尽模式,BulletMode[0]:"+this.planeObj.BulletMode[0]+" BulletMode[1]:"+this.planeObj.BulletMode[1]+" BulletMode[2]:"+this.planeObj.BulletMode[2]);
            if (FirstLaunch){
                FirstLaunch = false;
                gameObjectList.add(new BossObj("images/enemy3_n1.png", -200, -200, 0, this,2));
                gameObjectList.add(new Enemy2Obj("images/enemy2.png",-200,-200,0, this,2));
                gameObjectList.add(new EnemyObj("images/enemy1.png",-200,-200,0,this,2));
                gameObjectList.add(new PlaneObj("images/me1.png", -200, -200, 0, this,2));
            }
            //重新设定背景图应该在的高度
            if(!haveStartedGame) {
                bacGroundObj.setY(WINDOW_HEIGHT - BACKGROUND_IMAGE_HEIGHT + TOP_BORDER_HEIGHT); //背景重新设置Y值
                suspension.setLocation(WINDOW_WIDTH - SUSPENSION_LOGO_WIDTH, 30);
                resume.setLocation(WINDOW_WIDTH / 2 - RESUME_LOGO_HEIGHT / 2, WINDOW_HEIGHT / 2 - RESUME_LOGO_HEIGHT / 2);
                haveStartedGame = true;
            }
            //游戏速率随着时间增加变快
            if (count%5000==0&&this.FPS<900){
                this.FPS+=100;
                this.sleepTime = 1000 / this.FPS;
            }

            //我方战机升级
            if (this.bacGroundObj.score>250&&this.bacGroundObj.score<500){
                GameMusic.upgrade.play();
                this.planeObj.BulletMode[0]=false;
                this.planeObj.BulletMode[1]=true;
                this.planeObj.BulletMode_backup=this.planeObj.BulletMode.clone();
            }else if (this.bacGroundObj.score>800){
                GameMusic.upgrade.play();
                this.planeObj.BulletMode[0]=false;
                this.planeObj.BulletMode[1]=false;
                this.planeObj.BulletMode[2]=true;
                this.planeObj.BulletMode_backup=this.planeObj.BulletMode.clone();
            }

            //绘制所有游戏元素
            //将所有敌方元素添加
            enemyObjList.addAll(enemy1ObjList);
            enemyObjList.addAll(enemy2ObjList);
            //添加动画元素
            gameObjectList.addAll(animationObjs);
            for (GameObject object : gameObjectList){
                try {
                    object.paintSelf(gImage);
                }catch (Exception e){
                    object.DeBug();
                    e.printStackTrace();
                }

            }
            for (BombPropsObj propsObj:props_launchList)
                propsObj.paintSelf_launch(gImage);
            gameObjectList.removeAll(removeList);
            bulletObjList.removeAll(removeList);
            enemyBulletObjs.removeAll(removeList);
            enemyObjList.removeAll(removeList);
            enemy1ObjList.removeAll(removeList);
            enemy2ObjList.removeAll(removeList);
            animationObjs.removeAll(removeList);
            bombPropsObjs.removeAll(removeList);
            bulletObjList.removeAll(removeList);
            props_launchList.removeAll(removeList);

        }
        if(status == 6){
            gImage.setFont(new Font("微软雅黑", Font.BOLD, 20));
            gImage.setColor(Color.BLACK);
            gImage.drawImage(background, 0, 0, null);
            rankListNormalButton.paintSelf(gImage);
            rankListEndlessButton.paintSelf(gImage);
            rankListReturnButton.paintSelf(gImage);

            gImage.drawString("排名", WINDOW_WIDTH / 2 - 200, 150);
            gImage.drawString("分数", WINDOW_WIDTH / 2 - 50, 150);
            gImage.drawString("用户名", WINDOW_WIDTH / 2 + 100, 150);
            for(int i = 0;i < 10; i++){
                gImage.drawString(i + 1 + "", WINDOW_WIDTH / 2 - 200, 150 + (i + 1) * 50);
                gImage.drawString(rankListStringArray[i][0], WINDOW_WIDTH / 2 - 50, 150 + (i + 1) * 50);
                gImage.drawString(rankListStringArray[i][1], WINDOW_WIDTH / 2 + 100, 150 + (i + 1) * 50);
            }

        }

        //将缓冲区绘制好的图形整个绘制到容器的画布中
        g.drawImage(offScreenImage, 0, 0, null);
//        System.out.println("offScreenImage is drew");
    }

    //添加子弹或敌机
    public void createObj(){
        //随机生成道具
        //炸弹道具
        long time = System.currentTimeMillis();
        Random randomForProps = new Random();
        randomForProps.setSeed(time);
        int random1 = randomForProps.nextInt(20)+1;
        int random2 = randomForProps.nextInt(20)+1;
        int[] xLocation = {0,150,300};
        int randomLoaction = randomForProps.nextInt(3);
        if (count>100&&count%random1==0&&!PropsisExist&&PropsCD<=0){
            //移动方向随机和初始速度随机
            boolean direction = randomForProps.nextBoolean();
            int initSpeed = randomForProps.nextInt(10)+1;
            if (direction)
                initSpeed*=-1;
            bombPropsObjs.add(new BombPropsObj("images/bomb_supply.png",xLocation[randomLoaction],0,initSpeed,this));
            gameObjectList.add(bombPropsObjs.get(bombPropsObjs.size()-1));
            PropsisExist = true;
            PropsCD = PROPS_CD;
        }
        //子弹补给道具
        if (count>100&&count%random2==0&&!PropsisExist&&PropsCD<=0){
            //移动方向随机和初始速度随机
            boolean direction = randomForProps.nextBoolean();
            int initSpeed = randomForProps.nextInt(10)+1;
            if (direction)
                initSpeed*=-1;
            bulletPropsObjs.add(new BulletPropsObj("images/bullet_supply.png",xLocation[randomLoaction],0,initSpeed,this));
            gameObjectList.add(bulletPropsObjs.get(bulletPropsObjs.size()-1));
            PropsisExist = true;
            PropsCD = PROPS_CD;
        }
        //添加我方子弹
        //模式一
        //优化改10
        if (count%5==0&&this.planeObj.BulletMode[0]){
            GameMusic.bullet.play();
            bulletObjList.add(new BulletObj("images/bullet2.png",planeObj.x+51,planeObj.y-10,10,this,1));
            gameObjectList.add(bulletObjList.get(bulletObjList.size()-1));
        }
        //模式二
        if (count%5==0&&this.planeObj.BulletMode[1]){
            GameMusic.bullet.play();
            BulletObj leftB = new BulletObj("images/bullet2.png",planeObj.x+41,planeObj.y-10,10,this,1);
            leftB.speedX=0;
            BulletObj RightB = new BulletObj("images/bullet2.png",planeObj.x+61,planeObj.y-10,10,this,1);
            RightB.speedX=0;
            bulletObjList.add(leftB);
            bulletObjList.add(RightB);
            gameObjectList.add(leftB);
            gameObjectList.add(RightB);
        }
        //模式三
        //优化改10
        if (count%5==0&&this.planeObj.BulletMode[2]){
            GameMusic.supply.play();
            BulletObj leftB = new BulletObj("images/bullet2.png",planeObj.x+41,planeObj.y-10,10,this,1);
            leftB.speedX=-2;
            BulletObj MidB = new BulletObj("images/bullet2.png",planeObj.x+51,planeObj.y-10,10,this,1);
            MidB.speedX=0;
            BulletObj RightB = new BulletObj("images/bullet2.png",planeObj.x+61,planeObj.y-10,10,this,1);
            RightB.speedX=2;
            bulletObjList.add(leftB);
            bulletObjList.add(MidB);
            bulletObjList.add(RightB);
            gameObjectList.add(leftB);
            gameObjectList.add(MidB);
            gameObjectList.add(RightB);
        }
        //优化改20
        if (count%15==0){
            //生成敌方战斗机
            enemy1Count++;
            //地方战斗机生成时x坐标
            int x = new Random().nextInt(WINDOW_WIDTH / EnemyObj.ENEMY_1_WIDTH) * EnemyObj.ENEMY_1_WIDTH;
            enemy1ObjList.add(new EnemyObj("images/enemy1.png",x,0,5,this,1));
            gameObjectList.add(enemy1ObjList.get(enemy1ObjList.size() - 1));
        }
        //生成敌方2号战机
        //优化改100
        if (enemy1Count > 10 && count % (WINDOW_WIDTH / 80) == 0) {
            enemy2Count++;
            //地方战斗机生成时x坐标
            int x = new Random().nextInt(WINDOW_WIDTH / Enemy2Obj.ENEMY_2_WIDTH) * Enemy2Obj.ENEMY_2_WIDTH;
            Enemy2Obj enemy2Obj = new Enemy2Obj("images/enemy2.png",x,0,8, this,1);
            enemy2ObjList.add(enemy2Obj);
            gameObjectList.add(enemy2ObjList.get(enemy2ObjList.size() - 1));
        }
        if (enemy1Count > 30&&count%100==0)
            if (bossObj == null) {
                GameMusic.enemy3_flying.play();
                bossObj = new BossObj("images/enemy3_n1.png", 156, 0, 10, this,1);
                gameObjectList.add(bossObj);
            }

        //敌方子弹生成速度
        if ((count % 100 == 0||threeCount>0) && bossObj != null) {
            if (count%100==0)
                threeCount=3;
            FlipObj BossLaunchBullet = new FlipObj("images/enemy3_hit.png",this.bossObj.x,this.bossObj.y,0,this);
            gameObjectList.add(BossLaunchBullet);
            enemyBulletObjs.add(new EnemyBulletObj("images/bullet1.png", bossObj.x + 24, bossObj.y + 270, 5, this,1));
            enemyBulletObjs.add(new EnemyBulletObj("images/bullet1.png", bossObj.x + 144, bossObj.y + 270, 5, this,1));
            gameObjectList.add(enemyBulletObjs.get(enemyBulletObjs.size() - 2));
            gameObjectList.add(enemyBulletObjs.get(enemyBulletObjs.size() - 1));
            threeCount--;
        }
    }

    public static void main(String[] args) throws IOException {
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }

    public void printButton0(Graphics gImage){
        //每一次都要重新设置一边按钮的高，因为WINDOW_HEIGHT有可能会变化
        rankListButton.setY((int)(WINDOW_HEIGHT * 0.6));
        startGameButton.setY((int)(WINDOW_HEIGHT * 0.6) + 12);
        aboutUsButton.setY((int)(WINDOW_HEIGHT * 0.6));
        loggedInButton.setY((int)(WINDOW_HEIGHT * 0.6));
        registerButton.setY((int)(WINDOW_HEIGHT * 0.6));
        //添加文字(有个 + 10是为了更加美观)
        if (haveLoggedIn) {
            rankListButton.setX(LOGGED_IN_INTERVAL_LENGTH);
            startGameButton.setX(2 * LOGGED_IN_INTERVAL_LENGTH + OPTION_LOGO_SIDE_LENGTH);
            aboutUsButton.setX(3 * LOGGED_IN_INTERVAL_LENGTH + 2 * OPTION_LOGO_SIDE_LENGTH);
            startGameButton.paintSelf(gImage);
            rankListButton.paintSelf(gImage);
            aboutUsButton.paintSelf(gImage);
            gImage.drawString("玩家排行", LOGGED_IN_INTERVAL_LENGTH + 10, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
            gImage.drawString("开始游戏", (LOGGED_IN_INTERVAL_LENGTH * 2 + OPTION_LOGO_SIDE_LENGTH) + 10, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
            gImage.drawString("关于我们", (LOGGED_IN_INTERVAL_LENGTH * 3 + OPTION_LOGO_SIDE_LENGTH * 2) + 10, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
        } else {
            rankListButton.setX(NOT_LOGGED_IN_INTERVAL_LENGTH);
            loggedInButton.setX(NOT_LOGGED_IN_INTERVAL_LENGTH * 2 + OPTION_LOGO_SIDE_LENGTH);
            registerButton.setX(NOT_LOGGED_IN_INTERVAL_LENGTH * 3 + OPTION_LOGO_SIDE_LENGTH * 2);
            aboutUsButton.setX(NOT_LOGGED_IN_INTERVAL_LENGTH * 4 + OPTION_LOGO_SIDE_LENGTH * 3);
            rankListButton.paintSelf(gImage);
            loggedInButton.paintSelf(gImage);
            registerButton.paintSelf(gImage);
            aboutUsButton.paintSelf(gImage);
            gImage.drawString("玩家排行", NOT_LOGGED_IN_INTERVAL_LENGTH + 10, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
            gImage.drawString("登录", NOT_LOGGED_IN_INTERVAL_LENGTH * 2 + OPTION_LOGO_SIDE_LENGTH + 30, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
            gImage.drawString("注册", NOT_LOGGED_IN_INTERVAL_LENGTH * 3 + OPTION_LOGO_SIDE_LENGTH * 2 + 30, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
            gImage.drawString("关于我们", NOT_LOGGED_IN_INTERVAL_LENGTH * 4 + OPTION_LOGO_SIDE_LENGTH * 3 + 10, (int) (WINDOW_HEIGHT * 0.6 + OPTION_LOGO_SIDE_LENGTH + 25));
        }

    }

    public void printButton3(Graphics gImage){
        gameOverButton.setLocation(WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 - 50);
        againButton.setLocation(WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2);
        gameOverButton.paintSelf(gImage);
        againButton.paintSelf(gImage);
    }

    public void gameAgain(){
        //重新开始
        PropsisExist = false;
        count = 1;
        haveStartedGame = false;
        enemy1Count = 0;
        enemy2Count = 0;
        planeObj = new PlaneObj("images/me1.png", WINDOW_WIDTH / 2 - PlaneObj.MY_PLANE_WIDTH / 2, WINDOW_HEIGHT - PlaneObj.MY_PLANE_HEIGHT, 0, GameWin.this,1);
        BacGroundObj.score = 0;
        bulletObjList.clear();
        enemyBulletObjs.clear();
        gameObjectList.clear();
        enemyObjList.clear();
        enemy1ObjList.clear();
        enemy2ObjList.clear();
        removeList.clear();
        animationObjs.clear();
        bulletPropsObjs.clear();
        bombPropsObjs.clear();
        props_launchList.clear();
        PlaneObj.isDestroyed = false;
        pushFlag=true;
        MyPlaneDead = 4;
        bossDead = 12;
        bossObj = null;
        gameObjectList.add(bacGroundObj); //背景图片
        gameObjectList.add(planeObj); //我方飞机
        gameObjectList.add(suspension); //暂停按钮
        status = 1;
    }
}
